package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.common.dal.base.MD5;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.dal.IUserDataDAL;
import com.qire.manhua.model.viewModel.impl.PasswordChangeGuideViewModel;
import com.qire.manhua.model.vo.OkResponseVO;

public class PasswordChangeGuideService extends DataService {

    private final IUserDataDAL userDataDAL = WarehouseFactory.create(this, IUserDataDAL.class);
    private final PasswordChangeGuideViewModel viewModel;

    public PasswordChangeGuideService(PasswordChangeGuideViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**
     * 旧密码校验
     * @param oldPassword
     */
    public void validateOldPassword(String oldPassword) {
        userDataDAL.validateOldPassword(oldPassword);
    }
    @StrategyAnnotation(event = Url.ValidateOldPassword)
    public void validateOldPasswordCallback(OkResponseVO dataVO, boolean isCache) {
        //修改密码API调用后回调处理
        if(dataVO.isOK()) {
            viewModel.setValidateInfo(dataVO.validate, "pwd");
            viewModel.nextPage();
        }
    }

    /**
     * 获取验证码
     * @param phoneNumber 接收的手机号码
     */
    public void fetchVerificationCode(String phoneNumber) {
        userDataDAL.fetchVerificationCode(phoneNumber);
    }
    @StrategyAnnotation(event = Url.FetchVerificationCode)
    public void fetchVerificationCodeCallback(OkResponseVO okResponseVO, boolean isCache) {
        ToastHelper.showSnackBar("验证码已发送请注意查收！", Prompt.SUCCESS);
    }

    /**
     * 校验验证码
     * @param phoneNumber 接收的手机号码
     * @param verifyCode 接收的验证码
     */
    public void checkVerificationCode(String phoneNumber, String verifyCode) {
        userDataDAL.checkVerificationCode(phoneNumber, verifyCode);
    }
    @StrategyAnnotation(event = Url.CheckVerificationCode)
    public void checkVerificationCodeCallback(OkResponseVO responseOkVO, boolean isCache) {
        ToastHelper.showSnackBar("验证成功！", Prompt.SUCCESS);
        viewModel.setValidateInfo(responseOkVO.validate, "msgcode");
        viewModel.nextPage();
    }

    /**
     * 修改密码
     * @param phoneNumber 手机号码
     * @param newPassword 新密码
     * @param validateCode 服务器验证码，非短信验证码
     * @param validateType 验证类型
     */
    public void updatePasswordAction(String phoneNumber, String newPassword, String validateCode, String validateType) {
        String password = MD5.Md5(newPassword);
        userDataDAL.updatePassword(phoneNumber, password, password, validateCode, validateType);
    }
    @StrategyAnnotation(event = Url.UpdatePassword)
    public void updatePasswordCallback(OkResponseVO dataVO, boolean isCache) {
        //修改密码API调用后回调处理
        if(dataVO.isOK()) {
            ToastHelper.showSnackBar("密码修改成功！", Prompt.SUCCESS);
            viewModel.nextPage();
        } else {
            ToastHelper.showSnackBar("密码修改失败！", Prompt.ERROR);
        }
    }

}
